#!/usr/bin/env python
# -*- coding: utf-8 -*-
#===============================================================================
# Copyright 2011 zod.yslin
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# 
# Author: zod.yslin
# Email: 
# File Name: getclasstree.py
# Description: 
#   inspect includes two methods for working directly with class hierarchies.
#   
#   getclasstree(), creates a tree-like data structure using nested lists and 
#   tuples based on the classes it is given and their base classes. 
#   Each element in the list returned is either a tuple with a class and its base
#   classes, or another list containing tuples for subclasses.
# Edit History: 
#   2011-11-02    File created.
#===============================================================================
import inspect
import example

class C(example.B):
    pass

class D(C, example.A):
    pass

def print_class_tree(tree, indent=-1):
    if isinstance(tree, list):
        for node in tree:
            print_class_tree(node, indent+1)
    else:
        print '  ' * indent, tree[0].__name__
    return

if __name__ == '__main__':
    print 'A, B, C, D:'
    # Note that D appears twice, since it inherits from both C and A.
    print_class_tree(inspect.getclasstree([example.A, example.B, C, D]))
    # This time, D only appears in the output once
    print_class_tree(inspect.getclasstree([example.A, example.B, C, D],
                                      unique=True,
                                      ))
